package com.shelpe.services.sinbad.service.simbaproxy;

import java.util.ArrayList;
import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.service.simbaproxy.UserClient.UserClientFallback;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

@FeignClient(name = "${feign.simba.name}", url = "${feign.simba.url}", fallback = UserClientFallback.class)
public interface UserClient {

	
	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.simba.login.authsign.get", consumes = "application/json;charset=UTF-8")
	AuthsignResponse findAuthsign(@RequestParam("nick") String nick);
	
	@Data
	@EqualsAndHashCode(callSuper=true)
	public static class AuthsignResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_login_authsign_get_response")
		private AuthsignRes res;
	}
	
	@Data
	public static class AuthsignRes {
		@JsonProperty("subway_token")
		private String subwayToken;
	}
	
	/**
	 * 获取账户实时报表数据
	 * 
	 * @param nick
	 * @param date
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.simba.rtrpt.cust.get", consumes = "application/json;charset=UTF-8")
	GetRtrCustResponse findRtrCust(@RequestParam("nick") String nick, @RequestParam("the_date") String date);

	@Data
	@EqualsAndHashCode(callSuper=true)
	public static class GetRtrCustResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_rtrpt_cust_get_response")
		private GetRtrCustRes res;
	}

	@Data
	public static class GetRtrCustRes {
		@JsonProperty("results")
		private GetRtrCustDtos resultList;
	}

	@Data
	public static class GetRtrCustDtos {
		@JsonProperty("rt_rpt_result_entity_d_t_o")
		private List<GetRtrCustDto> result;
	}

	@Data
	public static class GetRtrCustDto {
		@JsonProperty("thedate")
		private String theDate;

		private String cpc;
		
		private String cpm;

		private String ctr;

		private String impression;

		private String roi;

		private String click;
		
		private String coverage;

		private String cost;
		@JsonProperty("carttotal")
		private String cartTotal;
		@JsonProperty("directcarttotal")
		private String directCartTotal;
		@JsonProperty("directtransaction")
		private String directTransaction;
		@JsonProperty("directtransactionshipping")
		private String directTransactionShipping;
		@JsonProperty("favitemtotal")
		private String favItemTotal;
		@JsonProperty("favshoptotal")
		private String favShopTotal;
		@JsonProperty("favtotal")
		private String favTotal;
		@JsonProperty("indirectcarttotal")
		private String indirectCartTotal;
		@JsonProperty("indirecttransaction")
		private String indirectTransaction;
		@JsonProperty("indirecttransactionshipping")
		private String indirectTransactionShipping;
		@JsonProperty("transactiontotal")
		private String transactionTotal;
		@JsonProperty("transactionshippingtotal")
		private String transactionShippingTotal;
	}

	/**
	 * 获取实时余额
	 * 
	 * @param nick
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.simba.account.balance.get")
	GetBalanceResponse findBalance(@RequestParam("nick") String nick);

	@Data
	public static class GetBalanceResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_account_balance_get_response")
		private GetBalanceRes res;
	}

	@Data
	public static class GetBalanceRes {
		private float balance;
	}

	/**
	 * 单品搜索人群修改状态
	 * 
	 * @param nick
	 * @param adgroupCrowdIDs
	 * @param adgroupID
	 * @param state
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.simba.serchcrowd.state.batch.update")
	UpdateSearchcrowdStateResponse updateSearchcrowdState(@RequestParam("nick") String nick,
			@RequestParam("adgroup_crowd_ids") List<Long> adgroupCrowdIDs, @RequestParam("adgroup_id") long adgroupID,
			@RequestParam("state") int state);

	@Data
	public static class UpdateSearchcrowdStateResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_serchcrowd_state_batch_update_response")
		private UpdateSearchcrowdStateRes res;
	}

	@Data
	public static class UpdateSearchcrowdStateRes {
		@JsonProperty("error_list")
		private JsonDtos errorList;
		@JsonProperty("adgrouptargetingtags")
		private UpdateSearchcrowdStateDtos targetingtagsList;
	}

	@Data
	public static class JsonDtos {
		@JsonProperty("json")
		private List<String> errorIDs;

		public void addError(String num) {
			if (this.errorIDs == null) {
				this.errorIDs = new ArrayList<String>();
			}
			this.errorIDs.add(num);
		}
	}

	@Data
	public static class UpdateSearchcrowdStateDtos {
		@JsonProperty("adgroup_targeting_tag_dto")
		private List<SearchCrowdDto> targetingtags;
	}

	/**
	 * 单品推广搜索人群修改溢价
	 * 
	 * @param nick
	 * @param adgroupCrowdIDs
	 * @param adgroupID
	 * @param discount
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.simba.serchcrowd.price.batch.update")
	UpdateSearchcrowdPriceResponse updateSearchcrowdPrice(@RequestParam("nick") String nick,
			@RequestParam("adgroup_crowd_ids") List<Long> adgroupCrowdIDs, @RequestParam("adgroup_id") long adgroupID,
			@RequestParam("discount") int discount);

	@Data
	public static class UpdateSearchcrowdPriceResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_serchcrowd_price_batch_update_response")
		private UpdateSearchcrowdPriceRes res;
	}

	@Data
	public static class UpdateSearchcrowdPriceRes {
		@JsonProperty("adgrouptargetingtags")
		private UpdateSearchcrowdPriceDtos targetingtagsList;
	}

	@Data
	public static class UpdateSearchcrowdPriceDtos {
		@JsonProperty("adgroup_targeting_tag_dto")
		private List<SearchCrowdDto> targetingtags;
	}

	/**
	 * 新建溢价人群
	 * 
	 * @param nick
	 * @param adgroupTargetingTags
	 * @param adgroupID
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.simba.searchcrowd.batch.add")
	CreateSearchcrowdResponse createSearchcrowd(@RequestParam("nick") String nick,
			@RequestParam("adgroup_targeting_tags") String adgroupTargetingTags,
			@RequestParam("adgroup_id") long adgroupID);

	@Data
	public static class CreateSearchcrowdResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_searchcrowd_batch_add_response")
		private CreateSearchcrowdRes res;
	}

	@Data
	public static class CreateSearchcrowdRes {
		@JsonProperty("adgrouptargetingtags")
		private CreateSearchcrowdDtos targetingtagsList;
	}

	@Data
	public static class CreateSearchcrowdDtos {
		@JsonProperty("adgroup_targeting_tag_dto")
		private List<SearchCrowdDto> targetingtags;

		public void addCrowd(SearchCrowdDto crowdDto) {
			if (this.targetingtags == null) {
				this.targetingtags = new ArrayList<SearchCrowdDto>();
			}
			this.targetingtags.add(crowdDto);
		}
	}

	/**
	 * 根据推广单元id获取搜索溢价人群
	 * 
	 * @param nick
	 * @param adgroupID
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.simba.serchcrowd.get")
	GetSearchCrowdResponse GetSearchcrowd(@RequestParam("nick") String nick,
			@RequestParam("adgroup_id") long adgroupID);

	@Data
	public static class GetSearchCrowdResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_serchcrowd_get_response")
		private GetSearchcrowdRes res;
	}

	@Data
	public static class GetSearchcrowdRes {
		@JsonProperty("adgrouptargetingtags")
		private GetSearchCrowdDtos targetingtagsList;
	}

	@Data
	public static class GetSearchCrowdDtos {
		@JsonProperty("result")
		private List<SearchCrowdDto> targetingtags;

		public void addCrowd(SearchCrowdDto crowdDto) {
			if (this.targetingtags == null) {
				this.targetingtags = new ArrayList<SearchCrowdDto>();
			}
			this.targetingtags.add(crowdDto);
		}
	}

	@Data
	@JsonInclude(Include.NON_NULL)
	public static class SearchCrowdDto {
		@JsonProperty("price_mode")
		private int priceMode;
		@JsonProperty("discount")
		private int discount; // 返回的溢价比例,乘的关系,discount=121,代表溢价21%
		@JsonProperty("is_default_price")
		private int isDefaultPrice; // 人群是否溢价1:不溢价;0:溢价
		@JsonProperty("online_status")
		private int onlineStatus; // 人群上下线状态0:暂停, 1:启用
		@JsonProperty("id")
		private long targetID; // 定向id,出价删除 改价,修改状态时用
		@JsonProperty("crowd")
		private Crowd crowds;

		@Data
		public static class Crowd {
			@JsonProperty("biz_type")
			private long bussiessType; // 业务类型
			@JsonProperty("cust_id")
			private long custID; // 用户id
			@JsonProperty("name")
			private String name; // 人群名称
			@JsonProperty("crowd_id")
			private long crowdID; // 人群id,报表用
			@JsonProperty("template_id")
			private long templateID; // 人群包的模板id
			@JsonProperty("type")
			private int type; // 人群类型
			@JsonProperty("fit_discount")
			private int fitDiscount; // 人群建议溢价,取值范围[5,300]
		}

		private SimbaProxyBaseDto.Error error;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.simba.serchcrowd.batch.delete")
	DelSearchcrowdResponse delSearchcrowd(@RequestParam("nick") String nick,
			@RequestParam("adgroup_crowd_ids") List<Long> adgroupCrowdIDs);

	@Data
	public static class DelSearchcrowdResponse extends SimbaProxyBaseDto {
		@JsonProperty("simba_serchcrowd_batch_delete_response")
		private DelSearchcrowdRes res;
	}

	@Data
	public static class DelSearchcrowdRes {
		@JsonProperty("delete_list")
		private JsonDtos deleteIDs;
		@JsonProperty("error_d_t_o_list")
		private JsonDtos errorIDs;
		@JsonProperty("args")
		private Args args;
		private String cause;
		private String key;
	}

	public static class Args {
		@JsonProperty("string")
		private List<String> strs;

		public void addString(String num) {
			if (this.strs == null) {
				this.strs = new ArrayList<String>();
			}
			this.strs.add(num);
		}
	}

	@Service
	@Slf4j
	public static class UserClientFallback implements UserClient {
		@Override
		public GetRtrCustResponse findRtrCust(String nick, String date) {
			log.error("fallback findRtrCust: {},{}", nick, date);
			GetRtrCustResponse ret = new GetRtrCustResponse();
			ret.setError(new SimbaProxyBaseDto.Error());
			ret.getError().setCode(-1);
			ret.getError().setMessage("fallback");
			return ret;
		}

		@Override
		public GetBalanceResponse findBalance(String nick) {
			log.error("fallback findRtrCust: {}", nick);
			GetBalanceResponse ret = new GetBalanceResponse();
			ret.setError(new SimbaProxyBaseDto.Error());
			ret.getError().setCode(-1);
			ret.getError().setMessage("fallback");
			return ret;
		}

		@Override
		public UpdateSearchcrowdStateResponse updateSearchcrowdState(String nick, List<Long> adgroupCrowdIDs,
				long adgroupID, int state) {
			log.error("fallback updateSearchcrowdState: {},{},{},{}", nick, adgroupCrowdIDs, adgroupID, state);
			UpdateSearchcrowdStateResponse ret = new UpdateSearchcrowdStateResponse();
			ret.setError(new SimbaProxyBaseDto.Error());
			ret.getError().setCode(-1);
			ret.getError().setMessage("fallback");
			return ret;
		}

		@Override
		public UpdateSearchcrowdPriceResponse updateSearchcrowdPrice(String nick, List<Long> adgroupCrowdIDs,
				long adgroupID, int discount) {
			log.error("fallback updateSearchcrowdPrice: {},{},{},{}", nick, adgroupCrowdIDs, adgroupID, discount);
			UpdateSearchcrowdPriceResponse ret = new UpdateSearchcrowdPriceResponse();
			ret.setError(new SimbaProxyBaseDto.Error());
			ret.getError().setCode(-1);
			ret.getError().setMessage("fallback");
			return ret;
		}

		@Override
		public CreateSearchcrowdResponse createSearchcrowd(String nick, String adgroupTargetingTags, long adgroupID) {
			log.error("fallback createSearchcrowd: {},{},{}", nick, adgroupTargetingTags, adgroupID);
			CreateSearchcrowdResponse ret = new CreateSearchcrowdResponse();
			ret.setError(new SimbaProxyBaseDto.Error());
			ret.getError().setCode(-1);
			ret.getError().setMessage("fallback");
			return ret;
		}

		@Override
		public GetSearchCrowdResponse GetSearchcrowd(String nick, long adgroupID) {
			log.error("fallback GetSearchcrowd: {},{}", nick, adgroupID);
			GetSearchCrowdResponse ret = new GetSearchCrowdResponse();
			ret.setError(new SimbaProxyBaseDto.Error());
			ret.getError().setCode(-1);
			ret.getError().setMessage("fallback");
			return ret;
		}

		@Override
		public DelSearchcrowdResponse delSearchcrowd(String nick, List<Long> adgroupCrowdIDs) {
			log.error("fallback delSearchcrowd: {},{}", nick, adgroupCrowdIDs);
			DelSearchcrowdResponse ret = new DelSearchcrowdResponse();
			ret.setError(new SimbaProxyBaseDto.Error());
			ret.getError().setCode(-1);
			ret.getError().setMessage("fallback");
			return ret;
		}

		@Override
		public AuthsignResponse findAuthsign(String nick) {
			// TODO Auto-generated method stub
			return null;
		}

	}
}
